/*
 * key.c
 *
 *  Created on: Apr 8, 2024
 *      Author: boen
 */
#include <key.h>


static uint8_t key1_state = 0;
static uint8_t key2_state = 0;

static int key1_tick = 0;
static int key2_tick = 0;

#define KEY_TICK_MAX 100

#define TEST_KEY		0
#if TEST_KEY
static uint8_t test_1 = 0;
static uint8_t test_2 = 0;
#endif




void key_timer(void)
{
	if(key1_tick)
	{
		key1_tick--;
	}

	if(key2_tick)
	{
		key2_tick--;
	}
}


/**
* @brief
* @param
* @retval
*/
int key_init(void)
{
	return 0;
}


/**
* @brief
* @param
* @retval
*/
int key_deinit(void)
{
	return 0;
}


/**
* @brief
* @param
* @retval
*/
int key_polling(void)
{
	if((HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin)==GPIO_PIN_RESET))
	{
		switch(key1_state)
		{
			case 0:
				key1_tick = KEY_TICK_MAX;
				key1_state = 1;
				break;
			case 1:
				if(key1_tick <= APP_NULL)
				{
					/*=====to do=====*/
#if TEST_KEY
					if(test_1>0)
					{
						mobile_move_x(DIS_X_2);
						test_1 = 0;
					}
					else
					{
						mobile_move_x(DIS_X_1);
						test_1 = 1;
					}
#else
					if(xbot.state == SYS_FLAG_IDLE)
					{
						xbot.state = SYS_FLAG_PLAY;
					}
#endif
					/*======end======*/
					key1_state = 2;
				}
				break;
		}
	}
	else
	{
		key1_state = APP_NULL;
		key1_tick = APP_NULL;

	}

	if((HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin)==GPIO_PIN_RESET))
	{
		switch(key2_state)
		{
			case 0:
				key2_tick = KEY_TICK_MAX;
				key2_state = 1;
				break;
			case 1:
				if(key2_tick <= APP_NULL)
				{
					/*=====to do=====*/
#if TEST_KEY
					if(test_2 > 0)
					{
						mobile_move_y(DIS_Y_2);
						test_2 = 0;
					}
					else
					{
						mobile_move_y(DIS_Y_1);
						test_2 = 1;
					}
#else
					if(xbot.state == SYS_FLAG_PLAY)
					{
						xbot.state = SYS_FLAG_IDLE;
						xbot.mobile.state = SYS_FLAG_IDLE;
					}
#endif
					/*======end======*/
					key2_state = 2;
				}
				break;
		}
	}
	else
	{
		key2_state = APP_NULL;
		key2_tick = APP_NULL;
	}
	return 0;
}

